import Vue from 'vue'
import Router from 'vue-router'
import Demo1 from './demo1/index'
import {getCookie} from '@/utils/index'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: () => import('@/pages/home/index')
    },
    {
      path: '/componentsview',
      name: 'Hell',
      component: () => import('@/pages/componentsview/index')
    },
    {
      path: '/shiyan',
      name: '试验场',
      component: () => import('@/pages/shiyan/index')
    },
    {
      path: '/goodscart',
      name: '购物车',
      component: () => import('@/pages/goodscart/index')
    },
    {
      path: '/test',
      name: 'test',
      component: () => import('@/pages/test/index')
    },
    {
      path: '/upimg',
      name: 'upimg',
      component: () => import('@/pages/test/testUp')
    },
    ...Demo1
  ]
})
//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //to代表将要访问的路径
  //from从哪个页面出发
  //next是一个函数，表示放行
  // if (to.path === '/login') return next();
  
  if(to.meta.requireAuth){
     //获取本地的token
    const tokenStr = getCookie('token');
    if(tokenStr){
      return next();
    } else {
      return next('/login')
    }
  }
  next()
})

export default router